<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-8-11
 * Time: 11:59
 */

namespace App\Admin\Renderables;

use App\Models\UserBalanceLog;
use App\Models\WeekCheckout;
use App\Models\WeekProfitReturn;
use Illuminate\Contracts\Support\Renderable;
use Dcat\Admin\Admin;
use Carbon\Carbon;
use Cache;
use App\Models\Deposite;
use App\Models\Withdrawal;
use App\Models\AdminUser;



class AdminUserHeaderRenderables implements Renderable
{

    public function render()
    {
        list($deposite, $deposite_today, $withdrawal, $withdrawal_today, $user_count, $today_user_count)   = Cache::remember('deposite', 1, function(){
            $deposite   = Deposite::get();
            $withdrawal = Withdrawal::get();
            $user_count = AdminUser::whereHas('roles', function($q){
                $q->where('id', 2);
            })->count();

            $today_user_count = AdminUser::whereHas('roles', function($q){
                $q->where('id', 2);
            })->whereBetween('created_at', [
                Carbon::now()->toDateString().' 00:00:00',
                Carbon::now()->toDateString().' 23:59:59'
            ])->count();

            return [
                $deposite->sum('usd_amount'),// 总投入

                $deposite->whereBetween('created_at', [
                    Carbon::now()->toDateString().' 00:00:00',
                    Carbon::now()->toDateString().' 23:59:59'
                ])->sum('usd_amount'),// 今日投入
                $withdrawal->sum('usd_amount'),// 总投出,

                $withdrawal->whereBetween('created_at', [
                    Carbon::now()->toDateString().' 00:00:00',
                    Carbon::now()->toDateString().' 23:59:59'
                ])->sum('usd_amount'),// 今日投出

                $user_count,
                $today_user_count
            ];
        });

        return view('admin.admin_user.header',compact(
            'deposite', 'withdrawal',
            'user_count', 'today_user_count',
            'deposite_today', 'withdrawal_today'
        ))->render();
    }


    /***
     * 代理产品
     * @return mixed
     */
    public function trade()
    {
        $user = Admin::user();

        $weekCheckout = WeekCheckout::where('user_id', $user->id)->orderByDesc('created_at')
            ->select([
                \DB::raw('SUM(profit_checkout) as profit_checkout'),
                \DB::raw('created_at as date'),
            ])->groupBy('date')->get();

        return $weekCheckout;
    }


    /***
     * 下级产品
     * @return mixed
     */
    public function agent()
    {
        $user = Admin::user();

        $agent = WeekProfitReturn::where('user_id', $user->id)
            ->where('ismail', 2)
            ->orderByDesc('created_at')
            ->select([
                \DB::raw('SUM(zt_volume) as zt_volume'),
                \DB::raw('SUM(yj_volume) as yj_volume'),
                \DB::raw('SUM(zt_profit) as zt_profit'),
                \DB::raw('SUM(yj_profit) as yj_profit'),
                \DB::raw('SUM(tj_profit) as tj_profit'),
                \DB::raw('created_at as date'),
            ])->groupBy('date')->get();

        return $agent;
    }


    /***
     * 数据合并
     * @return array
     */
    public function trade_to_agen_merge()
    {
        $trade = $this->trade();
        $agent = $this->agent();

        if (count($trade) <= 0){
            $aa = [];
            $aa[0]['profit_checkout'] = 0;
            $aa[0]['date']            = 0;

            $trade = collect($aa);
        }

        if (count($agent) <= 0){
            $agent->profit_checkout = 0;
            $agent->date            = 0;

            $bb = [];
            $bb[0]['profit_checkout'] = 0;
            $bb[0]['date']            = 0;

            $agent = collect($bb, true);
        }


        $newTrade = [];
        foreach (meiWeek() as $k=>$v){
            foreach ($trade as $kk=>$vv) {
                if (isset($vv->date)){
                    $d = $vv->date;
                }else{
                    $d = $vv['date'];
                }
                if ($v == Carbon::parse($d)->toDateString()){
                    $newTrade[$k]['profit_checkout']  = $vv->profit_checkout;
                    $newTrade[$k]['date']             = Carbon::parse($d)->toDateString();
                    break;
                }else{
                    $newTrade[$k]['profit_checkout'] = 0;
                    $newTrade[$k]['date']            = $v;
                }
            }
        }

        $newAgent = [];
        foreach (meiWeek() as $k=>$v){
            foreach ($agent as $kk2=>$vv2) {
                if (isset($vv2->date)){
                    $dd = $vv2->date;
                }else{
                    $dd = $vv2['date'];
                }

                if ($v == Carbon::parse($dd)->toDateString()){
                    $newAgent[$k]['profit_checkout']  = $vv2->zt_volume+$vv2->yj_volume+
                                                        $vv2->zt_profit+$vv2->yj_profit+
                                                        $vv2->tj_profit;
                    $newAgent[$k]['date']             = Carbon::parse($dd)->toDateString();
                    break;
                }else{
                    $newAgent[$k]['profit_checkout'] = 0;
                    $newAgent[$k]['date']            = $v;
                }
            }
        }

        return [$newTrade, $newAgent];
    }

}
